package second.dp;

public class Solution16 {
    public int findTargetSumWays(int[] nums, int target) {
        int sum = 0;
        for (int x : nums) {sum += x;
        }
        sum += target;
        if (sum < 0) return 0;
        if (sum % 2 != 0) return 0;
        sum >>= 1;
        int[] dp = new int[sum + 1];
        dp[0] = 1;
        for (int i = 0; i < nums.length; i++) {
            for (int j = sum; j >= nums[i]; j--) {
                dp[j] += dp[j - nums[i]];
            }
        }
        return dp[sum];
    }
}
